﻿//----------------------------------------------------------------
// <copyright file="WorkItemChangedReceiver.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System.ServiceModel;
using MptCodeLibrary.Configuration;
using MptCodeLibrary.Exceptions;
using MptCodeLibrary.TeamFoundation;

namespace MptCodeLibrary.WcfReceivers
{
    [ServiceContract(Namespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
    public interface IWorkItemChangedReceiver
    {
        [OperationContract(Action = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify")]
        [XmlSerializerFormat(Style = OperationFormatStyle.Document)]
        void Notify(string eventXml);
    }

    public class WorkItemChangedReceiver : IWorkItemChangedReceiver
    {
        public void Notify(string eventXml)
        {
            try
            {
                MptEventLogger.WriteTrace("Received the event from TFS: " + eventXml);

                GlobalListBuilder globalListBuilder = new GlobalListBuilder(new MptConfiguration(),
                                                                            new TeamFoundationFacade());
                globalListBuilder.UpdateGlobalList(eventXml);

                MptEventLogger.WriteTrace("Done processing the event.\r\n");
            }
            catch (MptException mptException)
            {
                MptEventLogger.WriteError("Could not process the work item changed event.  The error is: " +
                                          mptException);
            }
        }
    }
}